package com.bolt.admin.module.sys.dto.converter;

import com.bolt.admin.module.sys.dto.OrgDTO;
import com.bolt.admin.module.sys.entity.OrgEntity;
import com.bolt.common.reflect.ObjectUtil;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * Created by Administrator on 2018/11/27.
 */
public class OrgConverter {
    private OrgConverter() {
// do nothing
    }

    /**
     * Entity对象转换为DTO对象
     *
     * @param org DO对象
     * @return DTO对象
     */
    public static OrgDTO toDTO(OrgEntity org) {
        if (Objects.isNull(org)) {
            return null;
        }
        OrgDTO result = new OrgDTO();
        result.setId(org.getId());
        result.setName(org.getName());
        result.setCode(org.getCode());
        result.setAreaCode(org.getAreaCode());
        result.setAddress(org.getAddress());
        result.setZipCode(org.getZipCode());
        result.setPid(org.getId());
        result.setMaster(org.getMaster());
        result.setTel(org.getTel());
        result.setEmail(org.getEmail());
        result.setRemarks(org.getRemarks());
        result.setEnabled(org.getEnabled());
        result.setVersionNO(org.getVersionNO());
        // TODO
        return result;
    }

    /**
     * DTO对象转换为Entity对象
     *
     * @param org DO对象
     * @return VO对象
     */
    public static OrgEntity toEntity(OrgDTO org) {
        if (Objects.isNull(org)) {
            return null;
        }
        OrgEntity result = new OrgEntity();
        result.setId(org.getId());
        result.setName(org.getName());
        result.setCode(org.getCode());
        result.setAreaCode(org.getAreaCode());
        result.setAddress(org.getAddress());
        result.setZipCode(org.getZipCode());
        if(ObjectUtil.isNotNull(org.getPid())){
            result.setParent(new OrgEntity(org.getPid()));
        }
        result.setMaster(org.getMaster());
        result.setTel(org.getTel());
        result.setEmail(org.getEmail());
        result.setRemarks(org.getRemarks());
        result.setEnabled(org.getEnabled());
        result.setVersionNO(org.getVersionNO());
        // TODO
        return result;
    }

    /**
     * DTO对象转换为DO对象
     *
     * @param org DO对象
     * @return VO对象
     */
    public static OrgEntity toEntity(OrgEntity result, OrgDTO org) {
        if (Objects.isNull(org)) {
            return null;
        }

        result.setName(org.getName());
        result.setCode(org.getCode());
        result.setAreaCode(org.getAreaCode());
        result.setAddress(org.getAddress());
        result.setZipCode(org.getZipCode());
        result.setPid(org.getId());
        result.setMaster(org.getMaster());
        result.setTel(org.getTel());
        result.setEmail(org.getEmail());
        result.setRemarks(org.getRemarks());
        result.setEnabled(org.getEnabled());
        result.setVersionNO(org.getVersionNO());
// TODO
        return result;
    }

    /**
     * 批量 Entity对象转换为DTO对象
     *
     * @param orgList Entity集合对象
     * @return DTO对象
     */
    public static List <OrgDTO> toDTO(List <OrgEntity> orgList) {
        if (Objects.isNull(orgList)) {
            return null;
        }
        return orgList.stream().map(OrgConverter::toDTO).collect(Collectors.toList());
    }

}
